﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using HandyControl.Data;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Interop;
using System.Windows.Media.TextFormatting;
using WpfDemo.Common;
using WpfDemo.IRepository.Sys;
using WpfDemo.Models.Sys;
using WpfDemo.View.Sys.User;

namespace WpfDemo.ViewModel.Sys
{
    public partial class UserViewModel: BaseViewModel
    {
        IUserRepository _repository;
        IRoleRepository _roleRepository;
        public UserViewModel(IUserRepository repository, IRoleRepository roleRepository)
        {
            this._repository = repository;
            this.PageInfo.pageSize = 6;
            GetPageList();
            _roleRepository = roleRepository;
            
        }

        [ObservableProperty]
        private ObservableCollection<UserEntity> gridModelList;

        [ObservableProperty]
        private bool _isEnabled = true;

        [ObservableProperty]
        private UserEntity userEntity;

      
        [ObservableProperty]
        private string keyStr = string.Empty;

        /// <summary>
        /// 获取当前路径
        /// </summary>
        public string CurrentUserPermission
        {
            get
            {

                return System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName; 
            }
            set { CurrentUserPermission = value; }
        }

        [RelayCommand]
        private void GetPageList()
        {
            
            GridModelList = new ObservableCollection<UserEntity>();

            Expression<Func<UserEntity, bool>> Wherelambda = p => true;
            if (!string.IsNullOrEmpty(keyStr))
            {
                Wherelambda = Wherelambda.And(x => x.F_UserName.Contains(keyStr) || x.F_Name.Contains(keyStr));
            }
            Expression<Func<UserEntity, string>> OrderBylambda = c => c.F_Id;
            var pageList = _repository.GetPageList(this.PageInfo,Wherelambda,OrderBylambda);
            this.GetPageData(pageList, this.GridModelList);

                     
        }
        public void BindRole(ComboBox box)
        {
            var roles = _roleRepository.Query().OrderBy(m=>m.F_Sort).ToList();
           var lst = roles.Select(m => new Param() { ParaValue = m.F_Id, ParaText = m.F_Name }).ToList();
            BindProvider.BindCbx(box, lst); ;
        }

        [ObservableProperty]
        private Object selectedItem;

        [ObservableProperty]
        private bool canUserAddOrEdit;

        [RelayCommand]
        private async void ReSearch()
        {
            this.KeyStr=string.Empty;
            this.PageInfo.PageIndex = 1;

            GetPageList();
        }

        /// <summary>
        /// 搜索
        /// </summary>
        [RelayCommand]
        private async void Search()
        {
            //需置于1
            this.PageInfo.PageIndex = 1;         
            GetPageList();
        }



        [RelayCommand]
        private async void Add()
        {
            this.UserEntity= new UserEntity();
            this.UserEntity.F_Id = "";
            var result = WindowManager.ShowDialog("UserForm", new object[] { null });
        }

        [RelayCommand]
        private async void Edit()
        {
            if (this.SelectedItem == null)
            {
                MessageHelper.ShowWarning("请选择要修改的信息");
                return;
            }
            this.UserEntity = (UserEntity)this.SelectedItem;
            var pwd2 = DESEncrypt.Decrypt(this.UserEntity.F_Password);
            this.UserEntity.F_Password = pwd2;
            var result = WindowManager.ShowDialog("UserForm", new object[] { null });
        }

        [RelayCommand]
        private async void Save(object obj)
        {
            var ent = this.UserEntity;
            if (ent == null) return;
            if (string.IsNullOrEmpty(ent.F_Id))
            {
                ent.F_Id = Guid.NewGuid().ToString("N").ToUpper();
                var pwd = DESEncrypt.Encrypt(ent.F_Password);
                ent.F_Password = pwd;
                ent.F_CreateTime=DateTime.Now;
                _repository.Add(ent);
                
            }
            else
            {
                var pwd = DESEncrypt.Encrypt(ent.F_Password);
                ent.F_Password = pwd;
                _repository.Update(ent);
            }
           
            GetPageList();            
            WindowManager.CloseFrm("UserForm", null);
            MessageHelper.ShowSuccess("保存成功！");
        }

        [RelayCommand]
        private async void Del()
        {
            if (this.SelectedItem == null)
            {                
                MessageHelper.ShowWarning("请选择要删除的信息");
                return;
            }           
            this.UserEntity = (UserEntity)this.SelectedItem;
            if(MessageHelper.ShowConfirm())
            {
                _repository.Delete(UserEntity);
                GetPageList();
                MessageHelper.ShowSuccess("删除成功！");
            }
        }
        [RelayCommand]
        private async void PageUpdated(object obj)
        {
            GetPageList();
        }
    }
}
